/*
 * Copyright (c) 2002-2006 by OpenSymphony
 * All rights reserved.
 */
package org.fordprefect.struts2jscalendarplugin.components;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * <!-- START SNIPPET: javadoc -->
 * 
 * Adds the necessary script-includes to a html HEAD, to use the
 * struts2jscalendar-plugin.
 * 
 * 
 * <!-- END SNIPPET: javadoc -->
 * 
 * <p/>
 * <b>Examples</b>
 * 
 * <pre>
 * <!-- START SNIPPET: example1 -->
 * &lt;head&gt;
 *   &lt;title&gt;My page&lt;/title&gt;
 *   &lt;jscalendar:head/&gt;
 * &lt;/head&gt;
 * <!-- END SNIPPET: example1 -->
 * </pre>
 * 
 * To use calendar with german locale:
 * 
 * <pre>
 * <!-- START SNIPPET: example2 -->
 * &lt;head&gt;
 *   &lt;title&gt;My page&lt;/title&gt;
 *   &lt;jscalendar:head language="de"/&gt;
 * &lt;/head&gt;
 * <!-- END SNIPPET: example2 -->
 * </pre>
 * 
 * If you use this jscalendar based datepicker widget, you might want to use one
 * of the standard stylesheets provided with jscalendar (all distribution
 * stylesheets are included in struts2 jar). The easiest way to do so is to
 * place the &lt;jscalendar:head/&gt; tag in the head of your html page, as it
 * takes care of including calendar css. Otherwise, to manually activate the
 * calendar-green style, include the following in your stylesheet definition:
 * 
 * <pre>
 * <!-- START SNIPPET: example2 -->
 * &lt;head&gt;
 *   &lt;title&gt;My page&lt;/title&gt;
 *   &lt;jscalendar:head calendarcss="calendar-green.css"/&gt;
 * &lt;/head&gt;
 * <!-- END SNIPPET: example2 -->
 * </pre>
 * 
 * <p>
 * Available stylesheets are:
 * <ul>
 * <li>calendar-blue.css</li>
 * <li>calendar-blue2.css</li>
 * <li>calendar-brown.css</li>
 * <li>calendar-green.css</li>
 * <li>calendar-system.css</li>
 * <li>calendar-tas.css</li>
 * <li>calendar-win2k-1.css</li>
 * <li>calendar-win2k-2.css</li>
 * <li>calendar-win2k-cold-1.css</li>
 * <li>calendar-win2k-cold-2.css</li>
 * </ul>
 * </p>
 * 
 * @author Patrick Lightbody
 * @author Rainer Hermanns
 * @author Rene Gielen
 * @author Philipp Leusmann
 * @ww.tag name="head" tld-body-content="empty"
 *         tld-tag-class="com.opensymphony.webwork.views.jsp.ui.HeadTag"
 *         description="Render a chunk of HEAD for your HTML file"
 * @since 2.2
 */
public class Head extends UIBean {
	public static final String TEMPLATE = "jscalendar_head";

	private String calendarcss = "calendar-blue.css";
	protected String language;

	public Head(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}

	@Override
	protected String getDefaultTemplate() {
		return TEMPLATE;
	}

	@Override
	public void evaluateParams() {
		super.evaluateParams();

		if (calendarcss != null) {
			String css = findString(calendarcss);
			if (css != null && css.trim().length() > 0) {
				if (css.lastIndexOf(".css") < 0) {
					addParameter("calendarcss", css + ".css");
				} else {
					addParameter("calendarcss", css);
				}
			}
		}

		if (language != null) {
			addParameter("language", findString(language));
		} else {
			final Locale locale = (Locale) getStack().getContext().get(
					ActionContext.LOCALE);
			if (locale != null) {
				addParameter("language", locale.getLanguage());
			} else {
				addParameter("language", Locale.getDefault().getLanguage());
			}
		}

		addParameter("encoding", "UTF-8");
	}

	public String getCalendarcss() {
		return calendarcss;
	}

	/**
	 * The jscalendar css theme to use" default="calendar-blue.css
	 * 
	 * @ww.tagattribute required="false"
	 */
	public void setCalendarcss(String calendarcss) {
		this.calendarcss = calendarcss;
	}

	/**
	 * The language to use for the widget texts and localization presets.
	 * 
	 * @s.tagattribute required="false" type="String"
	 *                 default="The language of the current Locale"
	 */
	@StrutsTagAttribute(description = "The language to use for the widget texts and localization presets.", required = false, defaultValue = "The language of the current Locale", type = "String")
	public void setLanguage(String language) {
		this.language = language;
	}

}
